package com.rxmvp.utils;

import android.support.annotation.NonNull;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/6/21.
 */

public class PartUtil {
    private static PartUtil fragment;

    public static PartUtil newInstance() {
        if (fragment == null) {
            fragment = new PartUtil();
        }
        return fragment;
    }

    @NonNull
    public RequestBody createPartFromString(String descriptionString) {
        return RequestBody.create(MultipartBody.FORM, descriptionString);
    }

    /**
     * @param partName 参数名
     * @param file     文件
     * @param fileType 文件类型 image/*
     * @return
     */
    @NonNull
    public MultipartBody.Part prepareFilePart(String partName, File file, String fileType) {
        // https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
        // use the FileUtils to get the actual file by uri
//        File file = FileUtils.getFile(this, fileUri);

        // create RequestBody instance from file
        RequestBody requestFile = RequestBody.create(MediaType.parse(fileType), file);

        // MultipartBody.Part is used to send also the actual file name
        return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
    }
}
